<?php 
namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;

class Role extends Controller
{
	public function index()
	{
//		$list = db('role')->select();
//		return view('admin@role/index',['list'=>$list]);
//------------------------------------------------------------------------------------------------------
        $wwb = input('param.rname');
        if (empty($wwb)) {
            $list = DB::name('role')->paginate(3);
        } else {
            $list = DB::name('role')->where('name', 'like', $wwb.'%')->paginate(3);
        }
        $arr = array();
        //遍历添加上 权限信息
        foreach($list as $v){
            $nodes = DB::name('role_node')->where(array('rid'=>array('eq',$v['id'])))->select();
            $node = array();
            foreach($nodes as $value){
                $node[] = DB::name('node')->where(array('id'=>array('eq',$value['nid'])))->value('name');
            }
            $v['node'] = $node;
            $arr[] = $v;
        }
        $page = $list->render();
        //分配变量
        $this->assign("list",$arr);
        $this->assign("page",$page);
        //加载模板
        return view('admin@role/index');

//        foreach($list as $v){
//            $role_ids = DB::name('user_role')->field('rid')->where(array('uid'=>array('eq',$v['id'])))->select();
//            //定义空数组
//            $roles = array();
//            //遍历
//            foreach($role_ids as $value){
//                $roles[] = DB::name('role')->where(array('id'=>array('eq',$value['rid']),'status'=>array('eq',1)))->value('name');
//            }
//            $v['role'] = $roles; //将新得到角色信息放置到$v中
//            $arr[] = $v;
//        }

    }


	public function add()
    {
        return view('admin@role/add');
    }

	public function doadd()
    {
        $data = $_POST;
        $result = db('role')->insert($data);
        if (!$result) {
            $this->error('添加失败');
        }

        $this->success('新增成功','admin/Role/index');

        // return view('admin@con1/func1');
    }

    public function delete($id)
    {
        $result = db('role')->delete($id);
        if (!$result) {
            $this->error('删除失败');
        }
        $this->success('删除成功','admin/role/index');
    }

    public function edit($id)
    {
        $list = db('role')->field(['id','status','name','remark'])->find($id);
        return view('admin@/role/edit',['data'=>$list]);
    }

    public function update(Request $request)
    {
//         var_dump($request);
//         var_dump($_POST);
        $p = $request->param();
        // var_dump($p);
        $data = [
            'name' => $p['name'],
            'remark' => $p['remark'],
            'status' => $p['status'],
        ];
        $result = db('role')->where('id',$p['id'])->update($data);
        if ($result) {
            $this->success('修改成功','admin/role/index');
        }
        $this->error('修改失败');

    }

    public function nodelist($id)
    {
        $name = db('role')->field('name,id')->find($id);
        $role = db('node')->where('status=1')->select();
        $data = Db::field(['node.name n_name','node.id nid'])
            ->table('lamp_role role,lamp_node node,lamp_role_node rono')
            ->where('role.id=rono.rid and node.id=rono.nid','node.status=1')
            ->select($id);
        $arr =array();
        foreach ($data as $k => $v) {
            $arr[] = $v['nid'];
        }

//            var_dump($arr);exit;
//
//        var_dump($data);
//        var_dump($name);
//        var_dump($role);
//        var_dump($arr);exit;
        $this->assign("arr",$arr);
        $this->assign("name",$name);
        $this->assign("role",$role);
//        $this->assign("data",$data);
        return view('admin@/role/nodelist');
    }

    public function savenode($id)
    {
        if (empty($_POST))
        {
            $this->error('必须分配一个权限');
        }

        db('role_node')->where('rid='.$id)->delete();
        foreach ($_POST as $k => $v) {
            $data['rid'] = $id;
            $data['nid'] = $v;
            db('role_node')->insert($data);
        }

        $this->success('修改成功','admin/role/index');
    }

}













